package P150;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiangMu
 * @version \$Id: P38.java, v 0.1, 2024年07月06日 01:52 LiangMu Exp $$
 */
public class P38 {

    public boolean wordPattern(String pattern, String s) {
        char[] chars = pattern.toCharArray();
        String[] split = s.split(" ");
        if (chars.length != split.length) {
            return false;
        }
        Map<Character, String> c2Str = new HashMap<>();
        Map<String, Character> str2c = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (c2Str.containsKey(chars[i])) {
                if (!c2Str.get(chars[i]).equals(split[i])) {
                    return false;
                }
            } else {
                c2Str.put(chars[i], split[i]);
            }

            if (str2c.containsKey(split[i])) {
                if (!str2c.get(split[i]).equals(chars[i])) {
                    return false;
                }
            } else {
                str2c.put(split[i], chars[i]);
            }
        }
        return true;
    }


}
